package com.hbo;

import com.github.javafaker.Faker;
import org.influxdb.InfluxDB;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author songxulin
 * @date 2022/2/18
 */
public class MockWeatherData {

    public static void main(String[] args) throws InterruptedException {
        InfluxDBUtil influxDBUtil = new InfluxDBUtil("admin", "admin", "http://10.0.2.230:8086/", "mydb", null);
        Faker faker = new Faker(new Locale("zh-CN"));

        String[] areas = {"南","北","东","西"};

        Integer[] altitudes = new Integer[]{500,800,1000,1500};
        int index=0;
        while (true){
            System.out.println("插入数据"+index++);
            Map<String,String> tags = new HashMap<String,String>(16);
            int areas_index=  (int)Math.floor(Math.random()*areas.length);
            int altitudes_index=  (int)Math.floor(Math.random()*altitudes.length);
            tags.put("altitude",altitudes[altitudes_index]+"");
            tags.put("area",areas[areas_index]+"");

            Map<String,Object> fields = new HashMap<String,Object>(16);
            fields.put("temperature",faker.number().numberBetween(10,30));
            fields.put("humidity",faker.number().numberBetween(-10,10));
            influxDBUtil.insert("weather",tags,fields,System.currentTimeMillis(), TimeUnit.MILLISECONDS);

            Thread.sleep(1000);
        }
    }

}
